package localSearch;

import instrumentation.Condition;
import instrumentation.ConditionClass;
import instrumentation.ConditionIf;
import instrumentation.ConditionSwitch;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import model.StatementKind;
import model.TestClusterBuilder;
import ReadWGZ.ReadFilewgz;
import ga.GAInfo;
import util.DebugConfig;
import util.GAConfig;
import util.Log;
public abstract class LocalSearchPopulation implements LocalSearch {
	protected GAInfo     gaInfo;

	public final void setExtraInfo(GAInfo gaInfo) {
		this.gaInfo     = gaInfo;

	}
	public final Map<Integer, Condition> readFileWGZ(TestClusterBuilder TestCluster)throws  IOException, ClassNotFoundException{
		if(gaInfo.getMapBranchCondition()!=null)
			return gaInfo.getMapBranchCondition();
		Map<String,Class<?>> classesToTest=TestCluster.getClassToTest();
		
		Map<Integer, Condition> mapBranchCondition= new HashMap<Integer, Condition>();
		ReadFilewgz readFile = new ReadFilewgz();
		for (String cName : classesToTest.keySet()) {
			Class<?> clazz=(Class<?>) classesToTest.get(cName);
			String classFileName=clazz.getName();

			classFileName=classFileName.replace('.', '/');
			classFileName=classFileName+".wgz";
			String inst=GAConfig.getDirInstrumented().toString();
			inst=inst.replace('\\', '/')+"/";
			// classFileName="file:"+inst+classFileName.toString();
			 String classFileName_="file:./instrumented/"+classFileName.toString();
			 Map<String, ConditionClass>  clazzes= readFile.read(classFileName_);
			mapBranchCondition.putAll( readFile.buildInternalState(clazzes));
			//classFileName_="file:./instrumented/"+ "identity/URN.wgz";
			 clazzes= readFile.read(classFileName_);
			 mapBranchCondition.putAll( readFile.buildInternalState(clazzes));
			//	classFileName_="file:./instrumented/"+ "identity/URI.wgz";
				 clazzes= readFile.read(classFileName_);
				 mapBranchCondition.putAll( readFile.buildInternalState(clazzes));
		}
		return  mapBranchCondition;
	}
	public void displayConditionAndRecommendMethods( Map<Class<?>, List<StatementKind>>   methodsRecommended,Condition c,int trgtBranch) {
		 StringBuilder sb = new StringBuilder();
		 sb.append("\nCondition Info:\n");
//		Condition c= sorted_map.get(entry.getKey());
		sb.append("\t"+c.getCName() + " " +c.getMName() + " ");		
		if(c instanceof ConditionIf )				
			sb.append("[branch:"+((ConditionIf) c).getBranchId() + "] ");
		else{
			  sb.append("[branch:"+trgtBranch + "] ");
			}
		sb.append(c.getCallee1()+ " ");

		sb.append(c.getV1()==null ?"null":c.getV1().toString()+ " ");
		if(c instanceof ConditionIf){
			sb.append(c.getV2()==null?"null":c.getV2().toString());
			sb.append(" PredicateTpe: "+c.getType().toString());
		}
		else
			sb.append("\tcases: " + ((ConditionSwitch) c).getCases().get(trgtBranch));
		
		sb.append( c.getIsCallMethod()?" call Method":"" );
		if(c.getExtraInfo()!=null)
			sb.append(" ExtraInfo : "+ c.getExtraInfo());
		sb.append("\n");
		List<StatementKind> methodsList = new LinkedList<StatementKind>();
		if(methodsRecommended.size()==0)
			System.err.println("LocalSearchPopulation: There are no Recommended Statements BR : "+ trgtBranch);
		sb.append("Recommended Statements:\n");
			for(Class<?> clazz:methodsRecommended.keySet()){
				methodsList = methodsRecommended.get(clazz);
				sb.append("\nClass: "+ clazz.getName()+  "\n");
				if(methodsList!=null)
					sb.append("\t"+methodsList);
			}
			System.out.println(sb.toString());
			if(DebugConfig.HCPure)
				Log.log(sb.toString());
	}
	public void displayConditions( Condition c,int trgtBranch) {
		 StringBuilder sb = new StringBuilder();
		 sb.append("");
//		Condition c= sorted_map.get(entry.getKey());
		sb.append("\t"+c.getCName() + " " +c.getMName() + " ");		
		if(c instanceof ConditionIf )				
			sb.append("[branch:"+((ConditionIf) c).getBranchId() + "] ");
		else{
			  sb.append("[branch:"+trgtBranch + "] ");
			}
		sb.append(c.getCallee1()+ " ");

		sb.append(c.getV1()==null ?"null":c.getV1().toString()+ " ");
		if(c instanceof ConditionIf){
			sb.append(c.getV2()==null?"null":c.getV2().toString());
			sb.append(" PredicateTpe: "+c.getType().toString());
		}
		else
			sb.append("\tcases: " + ((ConditionSwitch) c).getCases().get(trgtBranch));
			
		System.out.println(sb.toString());

	}
}
